package com.heima.wemedia.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.saveChannelDTO;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.listChannelDTO;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author itlxc
 * @since 2024-07-26
 */
@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    private final WmChannelMapper wmChannelMapper;

    private final WmNewsService wmNewsService;


    /**
     * 保存频道
     * @param saveChannelDTO
     * @return
     */
    @Override
    public ResponseResult saveChannel(saveChannelDTO saveChannelDTO) {
        WmChannel wmChannel = this.lambdaQuery()
                            .eq(WmChannel::getName, saveChannelDTO.getName())
                            .one();
        if (ObjectUtil.isNotNull(wmChannel)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该频道已存在");
        }
        WmChannel channel = BeanUtil.copyProperties(saveChannelDTO, WmChannel.class);
        if (this.save(channel)) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR, "保存频道失败");
    }

    /**
     * 频道列表
     * @param listChannelDTO
     * @return
     */
    @Override
    public ResponseResult listChannels(listChannelDTO listChannelDTO) {
        Page<WmChannel> page = this.lambdaQuery()
                .like(StrUtil.isNotBlank(listChannelDTO.getName()), WmChannel::getName, listChannelDTO.getName())
                .eq(ObjectUtil.isNotNull(listChannelDTO.getStatus()), WmChannel::getStatus, listChannelDTO.getStatus())
                .orderByDesc(WmChannel::getCreatedTime)
                .page(new Page<>(listChannelDTO.getPage(), listChannelDTO.getSize()));
        PageResponseResult result = new PageResponseResult(listChannelDTO.getPage(), listChannelDTO.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Override
    public ResponseResult updateChannel(saveChannelDTO saveChannelDTO) {
        // 如果频道被引用则不能禁用
        if (ObjectUtil.equal(saveChannelDTO.getStatus(), false)) {
            Integer count = wmNewsService.lambdaQuery()
                                        .eq(WmNews::getChannelId, saveChannelDTO.getId())
                                        .count();
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该频道下存在文章，不可禁用");
            }
        }
        // 更新频道
        WmChannel wmChannel = this.lambdaQuery()
                                .eq(WmChannel::getName, saveChannelDTO.getName())
                                .one();
        if (ObjectUtil.isNotNull(wmChannel)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该频道已存在");
        }
        WmChannel channel = BeanUtil.copyProperties(saveChannelDTO, WmChannel.class);
        if (this.updateById(channel)) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR, "更新频道失败");
    }

    @Override
    public ResponseResult delChannel(Long id) {
        WmChannel wmChannel = this.getById(id);
        if (ObjectUtil.equal(wmChannel.getStatus(), true)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "只有禁用的频道才能删除");
        }
        this.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}

